/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 16:19:19
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-06 09:24:16
 * @FilePath: \cxl-driving-training\src\views\clueManagement\clue\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive } from 'vue'
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus'
import { DICT_DATA } from '@/utils/dict-data'
import { roleDelete, rolePage, rolePageInterface } from '@/apis/company/role'

export default class RoleData extends BaseData implements BaseMethod {
    operationDialog = reactive({
        visible: false,
        type: 'create',
        id: '',
        name: '',
        description: ''
    })
    dataPermissionDialog = reactive({
        visible: false,
        id: '',
        name: ''
    })
    followStatus = reactive<{ label: string; value: string }[]>([])
    searchData = {
        name: ''
    }

    constructor() {
        super(tableConfig)
        this.followStatus = DICT_DATA.get('followStatus')
        this.getData()
    }
    getData = (arg?: any) => {
        const params: rolePageInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        if (this.searchData.name) {
            params.name = this.searchData.name
        }
        rolePage(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total
                this.pageData.table.data.push(...res.data.result.records)
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchData = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchData = {
            name: ''
        }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'edit') {
            this.operationDialog.visible = true
            this.operationDialog.type = type
            this.operationDialog.id = data.id
            this.operationDialog.description = data.description
            this.operationDialog.name = data.name
        } else if (type === 'create') {
            this.operationDialog.visible = true
            this.operationDialog.type = type
            this.operationDialog.id = ''
        } else if (type === 'authorization') {
            this.dataPermissionDialog.visible = true
            this.dataPermissionDialog.id = data.id
            this.dataPermissionDialog.name = data.name
        } else if (type === 'delete') {
            ElMessageBox.confirm('确定要删除该角色?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    roleDelete(data.id).then(res => {
                        if (Number(res.data.code) === 200) {
                            ElMessage.success('删除成功')
                            this.getData()
                        }
                    })
                })
                .catch(() => {
                    // 取消
                })
        }
    }
    //监听编辑弹窗关闭
    onCloseOperationDialog = () => {
        this.operationDialog.visible = false
        this.getData()
    }
    //监听数据权限弹窗关闭
    onCloseDataPermissionDialog = () => {
        this.dataPermissionDialog.visible = false
    }
}
